package com.xyz.rok.tools.controller;

import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.model.Union;
import com.xyz.rok.tools.model.UnionMember;
import com.xyz.rok.tools.model.request.UnionRequest;
import com.xyz.rok.tools.service.UnionMemberService;
import com.xyz.rok.tools.service.UnionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/5/12 14:24
 **/
@Slf4j
@RestController
@RequestMapping("/api/union")
public class UnionController {
    @Autowired
    private UnionService unionService;
    @Autowired
    private UnionMemberService unionMemberService;

    @GetMapping("/list")
    public Result list(@RequestParam("serverId") Integer serverId) {
        return Result.success(unionService.selectUnionByServerId(serverId));
    }

    @PostMapping("/addUnionMember")
    public Result addUnion(@RequestBody UnionMember unionMember) {
        unionMember.setCreateTime(new Date());
        unionMemberService.insertSelective(unionMember);
        return Result.success();
    }

    @GetMapping("/getMyUnion")
    public Result myUnion(HttpServletRequest request) {
        return Result.success(unionService.selectUnionByUserId((Long) request.getAttribute("userId")));
    }

    @GetMapping("/getMyUnionCount")
    public Result myUnionCount(HttpServletRequest request) {
        return Result.success(unionService.selectUnionCountByUserId((Long) request.getAttribute("userId")));
    }

    @GetMapping("/getUnion")
    public Result getUnion(@RequestParam("id") Long id) {
        return Result.success(unionService.selectByPrimaryKey(id));
    }


    @PostMapping("/addUnion")
    @ResponseBody
    public Result addUnion(@RequestBody UnionRequest unionRequest) throws Exception {
        unionService.createUnion(unionRequest);
        return Result.success();
    }

    @PostMapping("/joinUnion")
    @ResponseBody
    public Result joinUnion(@RequestBody UnionRequest unionRequest) throws Exception {
        unionService.joinUnion(unionRequest);
        return Result.success();
    }

    @PostMapping("/quitUnion")
    @ResponseBody
    public Result quitUnion(@RequestBody UnionRequest unionRequest) throws Exception {
        return Result.success(unionService.quitUnion(unionRequest));
    }

    @PostMapping("/deleteUnion")
    @ResponseBody
    public Result deleteUnion(@RequestBody UnionRequest unionRequest) throws Exception {
        return Result.success(unionService.deleteUnion(unionRequest));
    }

    @PostMapping("/updateUnion")
    @ResponseBody
    public Result updateUnion(@RequestBody UnionRequest unionRequest) throws Exception {
        unionService.updateUnion(unionRequest);
        return Result.success();
    }
}
